﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Data;

namespace VisionarySystems.Library
{
    public abstract class DatabaseUpdater
    {
        protected SqlConnection Conn = null;
        protected VisionarySystems.Library.DatabaseUtils m_DatabaseUtils;
        protected const string SystemTableName = "SystemInfo";

        string databaseName = String.Empty;
//        int currentVersionNo;

        public DatabaseUpdater(string DataSource, string DatabaseName)
        {
            databaseName = DatabaseName;
            m_DatabaseUtils = new DatabaseUtils(DataSource);
//            currentVersionNo = CurrentVersionNo;
        }

        public int CheckDatabaseVersion(int CurrentVersionNo)
        {
            int Result ;

            try
            {
                Conn = m_DatabaseUtils.GetConnection(databaseName);
                Result = DoCheck(CurrentVersionNo);
            }
            finally
            {
                if (Conn.State == ConnectionState.Open)
                    Conn.Close();
            }
            return Result;
        }

        private int DoCheck(int CurrentVersionNo)
        {
            int DbVersionNo = GetDbVersionNo();
            int Result = DbVersionNo;
            if (DbVersionNo < CurrentVersionNo)
            {
                for (int UpdateVersionNo = DbVersionNo+1; UpdateVersionNo <= CurrentVersionNo; UpdateVersionNo++)
                    Result = UpdateToVersion(UpdateVersionNo);
                if (Result == CurrentVersionNo)
                    SetDbVersionNo(CurrentVersionNo);
            }
            return Result;
        }

        private void SetDbVersionNo(int CurrentVersionNo)
        {
            if (m_DatabaseUtils.CheckTableExists(Conn, SystemTableName))
            {
                try
                {
                    if (Conn.State != ConnectionState.Open)
                        Conn.Open();

                    // Set new version
                    SqlCommand cmdSetVersionNo = Conn.CreateCommand();
                    cmdSetVersionNo.CommandText = "Update " + SystemTableName + " set VersionNo = @VersionNo" ;
                    cmdSetVersionNo.Parameters.Add("@VersionNo", System.Data.SqlDbType.Int);
                    cmdSetVersionNo.Parameters["@VersionNo"].Value = CurrentVersionNo;
                    cmdSetVersionNo.ExecuteNonQuery();
                }
                finally
                {
                    if (Conn.State == ConnectionState.Open)
                        Conn.Close();
                }
            }
        }

        protected abstract int UpdateToVersion(int VersionNo);

        private int GetDbVersionNo()
        {
            int Result = 0;
            if (m_DatabaseUtils.CheckTableExists(Conn, SystemTableName))
            {
                try
                {
                    if (Conn.State != ConnectionState.Open)
                        Conn.Open();
                    SqlCommand cmdCheckTable = Conn.CreateCommand();
                    cmdCheckTable.CommandText = "Select VersionNo from " + SystemTableName;
                    SqlDataReader rdr = cmdCheckTable.ExecuteReader();
                    if (rdr.Read())
                        Result = int.Parse(rdr["VersionNo"].ToString());
                }
                finally
                {
                    if (Conn.State == ConnectionState.Open)
                        Conn.Close();
                }
            }
            return Result;
        }
    }
}
